<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <title>tabbar</title>
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  <meta name="description" content="" />
  <meta name="keywords" content="" />
  <link href="" rel="stylesheet" />
  <style>
    * {
      box-sizing: border-box;
    }

    html,
    body {
      margin: 0;
      padding: 0;
      width: 100%;
      height: 100%;
    }

    ul,
    li {
      list-style: none;
      margin: 0;
      padding: 0;
    }

    #root {
      height: 100%;
      display: flex;
      flex-direction: column;
    }

    main {
      flex: 1;
    }

    ul {
      height: 44px;
      display: flex;
      border-top: 1px solid #ccc;
    }

    ul>li {
      flex: 1;
      text-align: center;
      line-height: 44px;
    }

    ul>li.active {
      background: chocolate;
      color: #fff;
    }
  </style>
</head>

<body>
  <div id="root">
    <main>

      <component :is="componentId"></component>
      <!--代表我要去加载一个组件-->
    </main>

    <ul>
      <li v-for="(tab,index) in tabItems" @click="handleClick(index,tab.component)"
        :class="{active: curIndex === index}" :key="tab.component">
        {{tab.title}}
      </li>
    </ul>
  </div>
  <script src="../../vue.js"></script>
  <script>
    const tab1 = {
      template: `
        <div>page1</div>
      `,
      mounted() {
        console.log(`mounted1.`)
      },
    }
    const tab2 = {
      template: `
        <div>page2</div>
      `,
      mounted() {
        console.log(`mounted2.`)
      },
    }
    const tab3 = {
      template: `
        <div>page3</div>
      `,
      mounted() {
        console.log(`mounted3.`)
      },
    }

    var vm = new Vue({
      el: "#root",
      data: {
        curIndex: 0,
        componentId: 'tab1',
        tabItems: [
          {
            component: 'tab1',
            title: '首页'
          },
          {
            component: 'tab2',
            title: '搜索'
          },
          {
            component: 'tab3',
            title: '我的'
          },
        ]
      },
      methods: {
        handleClick(index, component) {
          this.curIndex = index;
          this.componentId = component
        }
      },
      components: {
        tab1,
        tab2,
        tab3
      }
    })
    // 去掉run...提示
    Vue.config.productionTip = false
  </script>
</body>

</html>